<!-- 回复整改日期弹窗 -->
<template>
  <px-dialog v-model="visible" class="equip-dialog-wrap" title="驳回确认" width="800px" @open="openDialog" @close="closeDialog">
    <px-form ref="formRef" :model="formValue" :rules="rules" label-position="right" label-width="120px">
      <px-row>
        <px-col :span="24">
          <px-form-item label="驳回原因" prop="rejectReason">
            <px-input
              v-model="formValue.rejectReason"
              type="textarea"
              :autosize="{ minRows: 2, maxRows: 4 }"
              maxlength="1000"
              placeholder="请输入驳回原因"
              clearable
            />
          </px-form-item>
        </px-col>
      </px-row>
    </px-form>
    <div style="margin-top: 56px; text-align: right">
      <px-button @click="closeDialog">取消</px-button>
      <px-button type="primary" @click="handleSubmit(formRef)">确认驳回</px-button>
    </div>
  </px-dialog>
</template>

<script setup lang="ts">
import { changeRectifyStatusDefect } from "@/api/defect";
import { PxMessage } from "px-ui";
import { computed, reactive, ref, toRefs } from "vue";

// 调用父级-关闭弹窗、查询列表方法
const emits = defineEmits(["closeDialog"]);
// 父级传过来的值
const props = defineProps(["dialogVisible", "missingId"]);

const visible = computed(() => props.dialogVisible);
// 页面本身的值
const formRef = ref();
const state = reactive<any>({
  formValue: {
    rejectReason: ""
  }
});
const { formValue } = toRefs(state);

// 表单校验规则
const rules = computed(() => ({
  rejectReason: [{ required: true, message: "驳回原因不能为空", trigger: "change" }]
}));

// 打开弹窗
const openDialog = async () => {
  formRef.value.resetFields();
};
// 关闭弹窗
const closeDialog = () => {
  emits("closeDialog");
};
// 提交
const handleSubmit = async (formEl: any) => {
  if (!formEl) return;
  await formEl.validate(async valid => {
    if (valid) {
      const params = {
        missingId: props.missingId,
        rejectReason: formValue.value.rejectReason,
        rectifyStatus: "REJECT"
      };
      await changeRectifyStatusDefect(params);
      PxMessage.success("驳回成功");
      emits("closeDialog");
    }
  });
};
</script>

<style lang="scss" scoped>
@import url("@/views/all/equip/scss/add-dialog.scss");
</style>
